//  This file is part of Swordbible - http://www.swordwarrior.net
//  Copyright (C) 2003-2009 Manu Mital mital.manu@gmail.com

//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2, or (at your option)
//  any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software Foundation,
//  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


// Search.cpp : implementation file
//

#include <stdafx.h>
#include "SwordBible.h"
#include "Search.h"
#include "SwordBibleDoc.h"
#include "SwordBibleView.h"
#include ".\search.h"
#include "MainFrm.h"
#include <regex.h> 




int CSearch::count = 0;
BOOL CSearch::isSearching = FALSE;
CString CSearch::toSearch = _T("");
sword::ListKey CSearch::searchResults = NULL; 
sword::VerseKey CSearch::scope;
int CSearch::flag;
int CSearch::searchType;
CTextProgressCtrl CSearch::m_progressCtrl;
CSearch* CSearch::pS;
CString CSearch::m_searchTextInit;
int  CSearch::ModuleType;                   // module type: Bibles, Commentaries, Books, Devotionals
UINT  CSearch::ModuleActive;                 // the index of the active module in the array


// CSearch dialog
IMPLEMENT_DYNAMIC(CSearch, CDialog)
CSearch::CSearch(CWnd* pParent, int moduleType, int moduleActive, CString initText)
	: CDialog(CSearch::IDD, pParent)	
{ 
	
    m_radioEntire = 0;
    m_radioMultiWord = 0;
	count++;
	m_IscaseSensitive = FALSE;
	m_searchText = _T("");
    m_searchTextInit = initText;
	parent = pParent;
    pS = this;

    ModuleType = moduleType;                   // module type: Bibles, Commentaries, Books, Devotionals
    
	ModuleActive = moduleActive;                 // the index of the active module in the array

	if(moduleType == AFX_MAINBIBLE) {
		pModule = CSwordBibleDoc::pRTFBibleModule[ModuleActive];
	    font = CBiblePage::font;
	}

	else if (moduleType == AFX_COMMENTARY) {
		pModule = CSwordBibleDoc::pRTFComModule[ModuleActive];
		font = CBottomPage::font;
	}

	else if (moduleType == AFX_GENBOOK) {
		pModule = CSwordBibleDoc::pRTFGenBooks[ModuleActive];
		font = CGenBooks::font;
	}

	
	else if (moduleType == AFX_DEVOTIONAL) {
		pModule = CSwordBibleDoc::pRTFDevModule[ModuleActive];
		font = CDevotionalPage::font;
	}


}

CSearch::~CSearch()
{
	searchResults.ClearList();
}

void CSearch::DoDataExchange(CDataExchange* pDX)
{
  CDialog::DoDataExchange(pDX);
  DDX_Control(pDX, IDC_LIST_SEARCHRESULTS, m_resultListControl);
  DDX_Control(pDX, IDC_COMBO_SEARCHSTART, m_searchStartControl);
  DDX_Control(pDX, IDC_COMBO_SEARCHEND, m_searchEndControl);
  DDX_Text(pDX, IDC_EDIT_FINDBOX, m_searchText);
  DDV_MaxChars(pDX, m_searchText, 50);
  DDX_Check(pDX, IDC_CHECK_MATCHCASE, m_IscaseSensitive);

  DDX_Radio(pDX,IDC_RADIO_ENTIRE, m_radioEntire);
  DDX_Radio(pDX,IDC_RADIO_MULTIWORD, m_radioMultiWord);

  DDX_Control(pDX, IDC_RICHEDIT_SEARCH_PREVIEW, m_resultPreview);
  DDX_Control(pDX, IDC_PROGRESS_SEARCH, m_progressCtrl);
  DDX_Control(pDX, IDCANCEL, m_btnCancel);
  DDX_Control(pDX, ID_BUTTON_SEARCH, m_btnSearch);

  DDX_Control(pDX, IDC_COMBO_START_CHAP, m_comboStartChap);
  DDX_Control(pDX, IDC_COMBO_END_CHAP, m_comboEndChap);
  DDX_Control(pDX, IDC_CREATE_INDEX, m_btnIndex);
  DDX_Control(pDX, IDC_STATIC_BOOK, m_bookBmp);
}


BEGIN_MESSAGE_MAP(CSearch, CDialog)
 ON_BN_CLICKED(ID_BUTTON_SEARCH, OnButtonSearch)
 ON_NOTIFY(NM_DBLCLK, IDC_LIST_SEARCHRESULTS, OnNMDblclkListSearchresults)
 ON_NOTIFY(NM_CLICK, IDC_LIST_SEARCHRESULTS, OnClickSearchResults)
 ON_BN_CLICKED(IDC_SEARCHRESULTS_VL, &CSearch::OnAddtoVerseList)
 ON_BN_CLICKED(IDCANCEL, &CSearch::OnClickCancel)
 ON_CBN_SELCHANGE(IDC_COMBO_SEARCHSTART, &CSearch::OnChangeStartBook)
 ON_CBN_SELCHANGE(IDC_COMBO_SEARCHEND, &CSearch::OnChangeEndBook)
 ON_BN_CLICKED(IDC_CREATE_INDEX, &CSearch::OnCreateIndex)
 ON_STN_CLICKED(IDC_STATIC_BOOK, &CSearch::OnClickBook)
 ON_WM_SIZE()
 ON_EN_SETFOCUS(IDC_EDIT_FINDBOX, &CSearch::OnFocusEditBox)
END_MESSAGE_MAP()


// CSearch message handlers

void CSearch::OnButtonSearch()
{

  if(isSearching == TRUE)
    return;

	// Upodate data before getting the options
	UpdateData(TRUE);


    // return if the user has specified nothing to search for
	if(m_searchText == _T("") || pModule == NULL) return;

	searchTerms.RemoveAll();	
	toSearch = _T("");
	CString temp = _T("");
	int i = 0;
	while(AfxExtractSubString(temp, m_searchText, i++  , _T(' ')))
	{
		temp.Remove(_T(' '));

		if(temp.GetLength())
		{
			toSearch = toSearch + _T(" ") + temp;
			searchTerms.Add(temp);
		}
	}

	
	m_resultListControl.DeleteAllItems();
	m_resultPreview.SetRTF(_T(""));

	// set the scope of the search
	SetSearchScope();

	// case sensitive search?
	if (m_IscaseSensitive == TRUE)	flag = 0;
	else flag = REG_ICASE;

	// phrase (-1) or multiword (-2)
   searchType = 0;
   int sel = GetCheckedRadioButton(IDC_RADIO_MULTIWORD,IDC_RADIO_OPTIMIZED);

   if(sel == IDC_RADIO_MULTIWORD) searchType = -2; 
   else if(sel == IDC_RADIO_PHRASE)  searchType = -1; 
   else if(sel == IDC_RADIO_RE)   searchType = 0; 
   else if(sel == IDC_RADIO_OPTIMIZED)  searchType = -4; 


   searchResults.ClearList();


	// call the search thread
  isSearching = TRUE;
  AfxBeginThread(Searcher,(LPVOID) pModule);
  m_btnCancel.SetWindowText(_T("Stop Search"));
  m_btnSearch.EnableWindow(FALSE);

 }

// called after the search is terminated
void CSearch::SearchFinished()
{
   m_btnCancel.SetWindowText(_T("Cancel"));
 

  for (searchResults = sword::BOTTOM; !searchResults.Error(); searchResults--) {
			//	Return Value : The index of the new item if successful or -1 otherwise.
			pModule->SetKey(searchResults);
			int Index = m_resultListControl.InsertItem(LVIF_TEXT,0, (CString) searchResults  , 0, 0, 0, NULL);
	}
	

	if(searchResults.Count()> 0)
	{
	m_resultListControl.SetItemState(0,LVIS_SELECTED, LVIS_SELECTED);

	//change the key
	sword::SWKey* key =  searchResults.getElement(0);
	pModule->SetKey(*key);


	// display the text
	CString display = font.GetHeaderRTF() +   _T("\\cf1\\b ") + (CString) key->getShortText() + _T(" \\b0\\cf2 ");
	
	CString renderText = (CString) pModule->RenderText();
	
	renderText.Replace(_T("\\par"), _T(" "));
	renderText.Replace(_T("\\cf6"), _T("\\cf9"));
	
	display = display + renderText + _T("\\par\\par ") +  _T("}");

	// footnote tags are enclosed between href tags in bibles. Footnotes use color cf5 in the color table 
	// x reference tags are enclosed between href tags in bibles. cross references use color cf6 in the color table 
	display.Replace(_T("<a href=\"\">*n"),  _T("\\cf5\\b *n"));
	display.Replace(_T("<a href=\"\">*x"),  _T("\\cf6\\b *x"));
	display.Replace(_T("</a>"), _T("\\cf2\\b0 "));
	display.Replace(_T("<a href=\"\">"),  _T("\\cf6\\b "));



	m_resultPreview.SetRTF(display);
	
		// highlight the search word in results
	HighLight();

	}
		

	CString count;
	count.Format(_T("%d results found. Double click search results to jump to a verse"),searchResults.Count());
	GetDlgItem(IDC_STATIC_COUNT)->SetWindowText(count);


    m_btnSearch.EnableWindow(TRUE);

}

UINT CSearch::Searcher(LPVOID dummy)
{

 //isSearching = TRUE; <-- calling it right before AfxBeginThread()
 sword::SWModule *p = (sword::SWModule *) dummy;

 if(ModuleType == AFX_MAINBIBLE || ModuleType == AFX_COMMENTARY)
  searchResults = p->Search((CStringA)toSearch,searchType,flag ,&scope, 0, &percentUpdate);

 else  searchResults = p->Search((CStringA)toSearch,searchType,flag , 0, 0, &percentUpdate);

  isSearching = FALSE;
  

  pS->SearchFinished();

  return 0;
}
	
void CSearch:: percentUpdate(char percent, void* userData)  {
  m_progressCtrl.SetPos(percent);
  m_progressCtrl.Invalidate();
}


BOOL CSearch::OnInitDialog()
{
	CDialog::OnInitDialog();

  usingSearchFramework = FALSE;

	// set search dialog title
  if(pModule)
		SetWindowText(_T("Searching: ") + (CString) pModule->Description());

  if(pModule) {
    if (pModule->hasSearchFramework() && pModule->isSearchOptimallySupported("God", -4, 0, 0)) 
    usingSearchFramework = TRUE;
  }

  if(usingSearchFramework == TRUE) m_btnIndex.SetWindowText(_T("Delete Index"));
  else m_btnIndex.SetWindowText(_T("Create Index"));


		// fill in the combo boxes that let user select a custom range	
	for(int i = 0; i < 66; i++) {
		m_searchStartControl.AddString(CSwordBibleDoc::bookCode[i][0]);
		m_searchEndControl.AddString(CSwordBibleDoc::bookCode[i][0]);
}

    // set the search start to Genesis
	m_searchStartControl.SetCurSel(0);
    OnChangeStartBook();

	// set the search end to Revelation
	m_searchEndControl.SetCurSel(66- 1);
    OnChangeEndBook();


	 // Add titles for search results list box
	m_resultListControl.InsertColumn(0,_T("Verse") );
	
	// set the width of the search results list box
	CRect listRect;
	m_resultListControl.GetWindowRect(listRect);
	int width = listRect.Width();
	m_resultListControl.SetColumnWidth(0, width);
	
	m_resultListControl.EnableScrollBar(SB_HORZ,ESB_DISABLE_BOTH);

     m_resultListControl.SetExtendedStyle(m_resultListControl.GetExtendedStyle()
		 | LVS_EX_FULLROWSELECT);


	 searchResults.ClearList(); // clear the list containing search results

   if(m_searchTextInit.GetLength() > 1)    {
     m_searchText = m_searchTextInit;
     UpdateData(FALSE);
   }

  m_radioEntire = 0;
   UpdateData(FALSE);
  

   return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX Property Pages should return FALSE

}


void CSearch::SetSearchScope()

{
	UpdateData();

	scope.ClearBounds();

	if(ModuleType != AFX_MAINBIBLE && ModuleType != AFX_COMMENTARY) 
		return;
	

	 int sel = GetCheckedRadioButton(IDC_RADIO_ENTIRE,IDC_RADIO_CUSTOM);

	if(sel == IDC_RADIO_ENTIRE)
  { scope.LowerBound("Genesis 1:1"); scope.UpperBound("Revelation 22:21"); }

		else if(sel == IDC_RADIO_NT )
    { scope.LowerBound("Matthew 1:1"); scope.UpperBound("Revelation 22:21"); }

		else if(sel == IDC_RADIO_OT )
    { scope.LowerBound("Genesis 1:1"); scope.UpperBound("Malachi 4:6"); }

		else if(sel == IDC_RADIO_CUSTOM)
	{ 
		int startBook = m_searchStartControl.GetCurSel();
		int endBook= m_searchEndControl.GetCurSel();
    
    int startChap = m_comboStartChap.GetCurSel();
    int endChap = m_comboEndChap.GetCurSel();

    // get the total number of verses in the end chapter
	  int totalVerses = CSwordBibleDoc::VerseCount[endBook][endChap];

    CString startKey;
    startKey.Format(_T("%s %d:%d"),CSwordBibleDoc::bookCode[startBook][0],
		startChap + 1, 1);

    CString endKey;
    endKey.Format(_T("%s %d:%d"),CSwordBibleDoc::bookCode[endBook][0],
		endChap + 1, totalVerses);
   
    sword::SWKey lower = (CStringA) startKey;
    sword::SWKey upper = (CStringA) endKey;


   scope.LowerBound( lower  );
   scope.UpperBound( upper  );
 
	}
	
}




void CSearch::OnNMDblclkListSearchresults(NMHDR *pNMHDR, LRESULT *pResult)
{
	// TODO: Add your control notification handler code here
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

	if (searchResults.Count() < 1)
	{ *pResult = 0; return; }

	int item = pNMListView->iItem; // item is the zero based index

	
	
	sword::SWKey* key =  searchResults.getElement(item);

	if(key->Error()) return;


	CWnd * pWnd = AfxGetMainWnd();



	if (ModuleType == AFX_MAINBIBLE) {
	CSwordBibleDoc::key = *key;

	// Change the display in bible, notes, commentaries
	
	CBiblePage::ActiveMainFrameCommand = _T("ShowChapter");
	pWnd->SendMessageToDescendants(ID_BIBLEPAGE_COMMAND, 0,0);

	if(CSwordBibleDoc::notesSynch)
	{
	CNotesPage::ActiveMainFrameCommand = _T("BibleSynch");
	pWnd->SendMessageToDescendants(ID_NOTES_COMMAND,0,0);
	}

	if(CSwordBibleDoc::bottomSynch)
	{
	CBottomPage::ActiveMainFrameCommand = ("BibleSynch");
	pWnd->SendMessageToDescendants(ID_COMMENTARY_COMMAND,0,0);
	}  
	

	CBiblePage::ActiveMainFrameCommand = _T("highlight");
	pWnd->SendMessageToDescendants(ID_BIBLEPAGE_COMMAND, 0,0);

	

	}

	else if (ModuleType == AFX_COMMENTARY) {

	CSwordBibleDoc::key = *key;
		
	CBiblePage::ActiveMainFrameCommand = _T("ShowChapter");
	pWnd->SendMessageToDescendants(ID_BIBLEPAGE_COMMAND, 0,0);

	CBottomPage::ActiveMainFrameCommand = ("BibleSynch");
	pWnd->SendMessageToDescendants(ID_COMMENTARY_COMMAND,0,0);

	CBiblePage::ActiveMainFrameCommand = _T("highlight");
	pWnd->SendMessageToDescendants(ID_BIBLEPAGE_COMMAND, 0,0);

	}

	else if(ModuleType == AFX_DEVOTIONAL)
	{
		CSwordBibleDoc::devotionKey = *key;
		CString t =  key->getText();

		int month = _tstoi( t.Mid(0, 2) );
		int day =  _tstoi ( t.Mid(3,2)  );				
		int year = ((CDevotionalPage*) parent)->year;

		((CDevotionalPage*) parent)->month = month;
		((CDevotionalPage*) parent)->day = day;
		 
		CTime T(year, month, day,0,0,0);
	
		((CDevotionalPage*) parent)->DDCalendarCtrl.SetCurSel(T);

		((CDevotionalPage*) parent)->showText();

	}

  else if(ModuleType == AFX_GENBOOK) {

    
	CSwordBibleDoc::GenBookKey = *key;
	((CGenBooks*) parent)->ShowChapter();

  }
			


	*pResult = 0;
}



void CSearch::OnClickSearchResults(NMHDR *pNMHDR, LRESULT *pResult)
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;

	if (searchResults.Count() < 1)
	{ *pResult = 0; return; }

	int item = pNMListView->iItem; // item is the zero based index
	
	sword::SWKey* key =  searchResults.getElement(item);

	if(key->Error()) return;

	//change the key
	pModule->SetKey(*key);

	// display the text
	CString display = font.GetHeaderRTF() +   _T("\\cf1\\b ") + (CString) key->getShortText() + _T(" \\b0\\cf2 ");
	
	CString renderText = (CString) pModule->RenderText();
	
	renderText.Replace(_T("\\par"), _T(" "));
	renderText.Replace(_T("\\cf6"), _T("\\cf9"));
	
	display = display + renderText + _T("\\par\\par ") +  _T("}");

	// footnote tags are enclosed between href tags in bibles. Footnotes use color cf5 in the color table 
	// x reference tags are enclosed between href tags in bibles. cross references use color cf6 in the color table 
	display.Replace(_T("<a href=\"\">*n"),  _T("\\cf5\\b *n"));
	display.Replace(_T("<a href=\"\">*x"),  _T("\\cf6\\b *x"));
	display.Replace(_T("</a>"), _T("\\cf2\\b0 "));
	display.Replace(_T("<a href=\"\">"),  _T("\\cf6\\b "));


	m_resultPreview.SetRTF(display);

		// highlight the search word in results
	HighLight();

	*pResult = 0;
}

void CSearch::OnAddtoVerseList()
{

	if(ModuleType != AFX_MAINBIBLE && ModuleType != AFX_COMMENTARY)
		return; // only biblical verses can be added to verse lists.

	if(searchResults.Count() < 1) // no search results
	{
		AfxMessageBox(_T("There are no search results to save to verse list"));
	    return;
	}
	

	// create the verse list window if it does not exist
	if (!CMainFrame::m_pModelessVL)
       CMainFrame::m_pModelessVL= new CVerseLists;

   if (!::IsWindow(CMainFrame::m_pModelessVL->GetSafeHwnd()))
	   CMainFrame::m_pModelessVL->Create(IDD_VERSELISTS, (CMainFrame*) AfxGetApp()->GetMainWnd() );


   CMainFrame::m_pModelessVL->SaveExternalResults(&searchResults);



}


void CSearch::HighLight()
{
	
   if(searchType == -1) //match whole word
	   HighLightWord(toSearch);

   else
   {	
	int terms = searchTerms.GetCount();
	for(int i = 0; i < terms; i++) HighLightWord(searchTerms.GetAt(i));
   }

	m_resultPreview.SetSel(-1,0); // deselect all 


}

void CSearch::HighLightWord(CString word)
{
	FINDTEXTEX ft1;      // FINDTEXTEX structure that would be used for searching for search terms in rich control
	ft1.chrg.cpMax = -1;
	ft1.chrg.cpMin = 0;

	m_resultPreview.SetSel(0,0); 
	
	BOOL stop = FALSE;
	ft1.lpstrText = word ;
	int count = word.GetLength();

	while(stop == FALSE)
	{
	
	 int foundAt = m_resultPreview.FindText(FR_DOWN , &ft1); 	
	
	  if(foundAt != -1) {	                                // select & highlight the search word
		m_resultPreview.SetSel( foundAt, foundAt + count );
		m_resultPreview.SetSelColor(RGB(255,0,0));
	
		m_resultPreview.SetSel( foundAt, foundAt + count );
		m_resultPreview.SetSelectionBold();

		ft1.chrg.cpMin = foundAt + 1;	
	     } 

	    else stop = TRUE;

	}

}
void CSearch::OnClickCancel()
{
  if(isSearching == TRUE)
  {
    pModule->terminateSearch = TRUE;
       return;
  }

 OnCancel();
}

void CSearch::OnChangeStartBook()
{

  if(m_radioEntire != 3) {
   m_radioEntire = 3;
   UpdateData(FALSE);
  }

  	// the 0 based index of the new book that the user has selected
	int selectedBook  = m_searchStartControl.GetCurSel();
	
	// empty the strings in the chapter combo box 
  while(m_comboStartChap.GetCount() > 0)
	m_comboStartChap.DeleteString(0);

   // get the total number of chapters in the newly selected book
	int totalChap =_tstoi(CSwordBibleDoc::bookCode[selectedBook][2]);


   // add the "chapter number" strings in the chapter combo box
	CString indexString;
	for(int nni = 0; nni < totalChap ; nni++)  // nni = new index
	{ 
		indexString.Format(_T("%d"),nni + 1);
		m_comboStartChap.AddString(indexString);
	}

	m_comboStartChap.SetCurSel(0);

}

void CSearch::OnChangeEndBook()
{
  if(m_radioEntire != 3) {
   m_radioEntire = 3;
   UpdateData(FALSE);
  }

  	// the 0 based index of the new book that the user has selected
	int selectedBook  = m_searchEndControl.GetCurSel();
	
	// empty the strings in the chapter combo box 
  while(m_comboEndChap.GetCount() > 0)
	m_comboEndChap.DeleteString(0);

   // get the total number of chapters in the newly selected book
	int totalChap =_tstoi(CSwordBibleDoc::bookCode[selectedBook][2]);


   // add the "chapter number" strings in the chapter combo box
	CString indexString;
	for(int nni = 0; nni < totalChap ; nni++)  // nni = new index
	{ 
		indexString.Format(_T("%d"),nni + 1);
		m_comboEndChap.AddString(indexString);
	}

	m_comboEndChap.SetCurSel(0);
  
}

void CSearch::OnCreateIndex()
{
  if (usingSearchFramework) {
		if (MessageBox(_T("Delete Index?  Are you sure?"), _T("Delete Index"), MB_YESNO) == IDYES)
			pModule->deleteSearchFramework();
	}
	else {
			pModule->createSearchFramework(percentUpdate, NULL);
		}


  usingSearchFramework = FALSE;

  if(pModule) {
    if (pModule->hasSearchFramework() && pModule->isSearchOptimallySupported("God", -4, 0, 0)) 
    usingSearchFramework = TRUE;
  }

  if(usingSearchFramework == TRUE) m_btnIndex.SetWindowText(_T("Delete Index"));
  else m_btnIndex.SetWindowText(_T("Create Index"));
  
}


void CSearch::OnClickBook()
{
  RECT rc;
  m_bookBmp.GetWindowRect(&rc);
 // m_bookBmp.ClientToScreen(&rc); // TrackPopupMenu uses screen coords

   CMenu menu;
   menu.CreatePopupMenu();
   CMenu* pPopup = NULL;
   pPopup = &menu;

   if(pPopup == NULL) return;
   int dwSelectionMade; 

   UINT total = 0;
   sword::SWModule** modules = NULL;

   if(ModuleType == AFX_MAINBIBLE)
   {
     total = CSwordBibleDoc::numBiblesAvailable;
     modules = &CSwordBibleDoc::pRTFBibleModule[0];
   }

   else if(ModuleType == AFX_COMMENTARY)
   {
     total = CSwordBibleDoc::numComAvailable;
     modules = &CSwordBibleDoc::pRTFComModule[0];
   }

    else if(ModuleType == AFX_GENBOOK)
   {
	   total = CSwordBibleDoc::numGenBooksAvailable;
	   modules = &CSwordBibleDoc::pRTFGenBooks[0];
   }



   if(total == 0) return;


 
   // fill in the strings for different bible versions
	for(int i = 0; i < CSwordBibleDoc::numBiblesAvailable; i++)
		if(modules[i] != NULL)
		{
			     pPopup->AppendMenu(MF_STRING,ID_PLACEHOLDER + i, (CString) modules[i]->Description());
					 if(ModuleActive == i) pPopup->CheckMenuItem(ID_PLACEHOLDER + i,MF_CHECKED); 

		}


         dwSelectionMade =  pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON|TPM_RETURNCMD, 
         rc.left, rc.bottom + 4, this);

	
		int selection = dwSelectionMade;
		int base = ID_PLACEHOLDER;
		int temp = selection - base;


	if(temp >= 0 && temp < total) 
	{
		 ModuleActive = temp;
	   pModule = modules[ModuleActive];
     SetWindowText(_T("Searching: ") + (CString) pModule->Description());
	}

    pPopup->DestroyMenu(); 
}

void CSearch::OnSize(UINT nType, int cx, int cy)
{
  CDialog::OnSize(nType, cx, cy);

}

void CSearch::OnFocusEditBox()
{
	 m_searchText = _T("");
   UpdateData(FALSE);
}
